package com.itfreer.gis.gps.analysis;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.itfreer.gis.analysis.IGisFileAnalysis;
import com.itfreer.gis.graphic.GisGraphic;
import com.itfreer.gis.graphic.GisPoint;
import com.itfreer.gis.graphic.GisPolyg;
@Component("gps")
public class TxtAnalysis  implements IGisFileAnalysis {

	@Override
	public List<GisGraphic> analysis(String filePath) throws Exception {
		
		File file=new File(filePath);
		String str= this.txt2String(file);
		GisPolyg nGraphic=new GisPolyg();
		String[] strPonts=str.split("\n");
		if(strPonts!=null && strPonts.length>0) {
			for(String line:strPonts) {
				if(line!=null && !"".equals(line.trim())) {
					String[] iStr=line.split(",");
					if(iStr.length>3) {
						GisPoint point=new GisPoint();
						point.setX(Double.parseDouble(iStr[1]));
						point.setY(Double.parseDouble(iStr[2]));
						nGraphic.addPoint(point);
					}
				}
			}
		}
		
		List<GisGraphic> rGraphic=new ArrayList<>();
		rGraphic.add(nGraphic);
		return rGraphic;
	}
	
	/**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }

}
